<?php
declare (strict_types=1);

namespace app\api\model;

use think\Model;
use think\model\concern\SoftDelete;

/**
 * @mixin \think\Model
 */
class Goods extends Model
{
    use SoftDelete;
    public $table = 'pyg_goods';
    // 获取指定商品信息
    public static function getGoods($id)
    {
        return self::where('type_id', $id)->find();
    }
    // 商品信息分页
    public static function goodsInfo($page)
    {
        return self::paginate(10);
    }
    // 通过id获取数据
    public static function getGoodsById($id)
    {
        return self::find($id);
    }
    // 商品分类模型关联
    public function category()
    {
        return $this->hasOne(Category::class, 'id', 'cate_id')->bind(['cate_name'])->field('id,cate_name,pid_path_name');
    }
    // 商品品牌模型关联
    public function brand()
    {
        return $this->hasOne(Brand::class, 'id', 'brand_id');
    }
    // 商品照片模型关联
    public function images()
    {
        return $this->hasMany(Images::class, 'goods_id', 'id');
    }
    // 商品类型模型关联
    public function type()
    {
        return $this->hasOne(Type::class, 'id', 'type_id');
    }
    // 商品属性模型关联
    public function attrs()
    {
        return $this->hasMany(Attribute::class, 'type_id', 'type_id');
    }
    // 商品规格模型关联
    public function specGoods()
    {
        return $this->hasMany(Spec_goods::class, 'goods_id', 'id');
    }
    // 添加商品信息
    public static function addGoods($data)
    {
        return self::create($data);
    }
    // 删除商品
    public static function delGoods($id)
    {
        return self::destroy($id);
    }

    public static function updateGoods($data,$id){
        return self::update($data,['id'=>$id]);
    }

    public static function getGoodsList($id,$where=[]){
        return self::where($where)->paginate([
            'list_row'=>10,
            'query'=>['id'=>$id]
        ]);
    }


}
